package com.fizzkai.p2p.core.service.impl;

import com.fizzkai.p2p.core.domain.RealAuth;
import com.fizzkai.p2p.core.domain.UserInfo;
import com.fizzkai.p2p.core.exception.DisplayableException;
import com.fizzkai.p2p.core.mapper.RealAuthMapper;
import com.fizzkai.p2p.core.query.QueryObject;
import com.fizzkai.p2p.core.service.IRealAuthService;
import com.fizzkai.p2p.core.service.IUserInfoService;
import com.fizzkai.p2p.core.util.AssertUtil;
import com.fizzkai.p2p.core.util.BitStateUtil;
import com.fizzkai.p2p.core.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class RealAuthServiceImpl implements IRealAuthService {

    @Autowired
    private RealAuthMapper authMapper;
    @Autowired
    private IUserInfoService userInfoService;

    @Override
    public int saveOrUpdate(RealAuth entity) {
        int row;
        if (entity.getId() == null) {
            row = authMapper.insert(entity);
        } else {
            row = authMapper.updateByPrimaryKey(entity);
        }
        return row;
    }

    @Override
    public RealAuth get(Long id) {
        return authMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo<RealAuth> query(QueryObject qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        List<RealAuth> list = authMapper.queryList(qo);
        return new PageInfo<>(list);
    }

    @Override
    public void apply(RealAuth temp) {
        // 如果已认证 / 正在审核，就报错
        UserInfo current = userInfoService.getCurrent();
        if (current.isHasVideoAuth()) {
            throw new DisplayableException("您已经通过认证了，无需再次提交");
        }
        if (current.getRealAuthId() != null) {
            throw new DisplayableException("您申请的认证正在审核中，无需再次提交");
        }
        // 设置后台设置的 内容
        RealAuth auth = new RealAuth();
        auth.setApplier(UserContext.getCurrentLoginInfo());
        auth.setApplyTime(new Date());
        auth.setState(RealAuth.STATE_NORMAL);
        // 设置 前台传过来的 参数
        auth.setRealName(temp.getRealName());
        auth.setSex(temp.getSex());
        auth.setBornDate(temp.getBornDate());
        auth.setIdNumber(temp.getIdNumber());
        auth.setImage1(temp.getImage1());
        auth.setImage2(temp.getImage2());
        auth.setAddress(temp.getAddress());

        // 设置 进 数据库
        authMapper.insert(auth);
        // 管理 userInfo 对象
        current.setRealAuthId(auth.getId());
        userInfoService.update(current);
    }

    @Override
    public void audit(Long id, Integer state, String remark) {
        AssertUtil.isNotNull(id, "审核id 不能为空");
        AssertUtil.isNotNull(state, "审核状态 不能为空");
        RealAuth auth = get(id);
        if (auth == null) {
            throw new DisplayableException("没有该审核单");
        }
        if (auth.getState() != RealAuth.STATE_NORMAL) {
            throw new DisplayableException("该实名认证已经审核过了，状态为:" + auth.getStateDisplay());
        }
        // 设置 后台设置的 内容
        auth.setAuditor(UserContext.getCurrentLoginInfo());
        auth.setAuditTime(new Date());
        // 设置 备注 / state
        auth.setRemark(remark);
        UserInfo applier = userInfoService.get(auth.getApplier().getId());
        switch (state) {
            case RealAuth.STATE_PASS:
                // 审核通过
                auth.setState(RealAuth.STATE_PASS);
                // 设置 身份信息
                applier.setIdNumber(auth.getIdNumber());
                applier.setRealName(auth.getRealName());
                // 设置状态
                applier.addState(BitStateUtil.HAS_REALAUTH);
                break;
            default:
                // 审核失败
                auth.setState(RealAuth.STATE_REJECT);
                applier.setRealAuthId(null);
        }
        // 保存 前端客户信息
        userInfoService.update(applier);
        saveOrUpdate(auth);
    }
}
